package com.tmsps.ne4Weixin.api;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmsps.ne4Weixin.config.WxConfig;
import com.tmsps.ne4Weixin.utils.HttpClient;

/**
 * 
 * @author zhangwei 396033084@qq.com
 */
public class UserTagAPI extends BaseAPI {
	private static String BATCHTAGGING = BaseURL.concat("/cgi-bin/tags/members/batchtagging?access_token=%s");
	private static String BATCHUNTAGGING = BaseURL.concat("/cgi-bin/tags/members/batchuntagging?access_token=%s");
	private static String GETIDLIST = BaseURL.concat("/cgi-bin/tags/getidlist?access_token=%s");

	public UserTagAPI(WxConfig config) {
		super(config);
	}

	public JSONObject addTagToMember(int tagId, List<String> openid_list) {
		JSONObject tagJson = new JSONObject();
		tagJson.put("tagid", tagId);
		tagJson.put("openid_list", openid_list);
		String result = HttpClient.postJson(String.format(BATCHTAGGING, config.getAccessToken()), tagJson.toJSONString());
		return JSONObject.parseObject(result);
	}

	public JSONObject cancleMemberTag(int tagId, List<String> openid_list) {
		JSONObject tagJson = new JSONObject();
		tagJson.put("tagid", tagId);
		tagJson.put("openid_list", openid_list);
		String result = HttpClient.postJson(String.format(BATCHUNTAGGING, config.getAccessToken()), tagJson.toJSONString());
		return JSONObject.parseObject(result);
	}

	public JSONArray getMemberTagList(String openid) {
		JSONObject tagJson = new JSONObject();
		tagJson.put("openid", openid);
		String result = HttpClient.postJson(String.format(GETIDLIST, config.getAccessToken()), tagJson.toJSONString());
		JSONObject end = JSONObject.parseObject(result);
		return end.getJSONArray("tagid_list");
	}

}
